<template src="./template.html"></template>
<script>
import checkForm from 'mixins/checkform'
import { validateOrgName, validatePwd, validateUseLabel } from 'utils/ui-utils'
import { EXPIRED_STATUS_SIMPLIFY } from '../../consts'
import { addEmployee, updateEmployee, getEmployeeInfo } from '../models'
import { checkUsername } from '../../models'

export default {
  data() {
    return {
      EXPIRED_STATUS: EXPIRED_STATUS_SIMPLIFY,

      id: '',
      isPageLoading: false,
      OriginalData: {},
      basicForm: {
        nickName: '',
        pass: '',
        username: '',
        enabled: 0,
        userType: 2, // 1 管理员 2 企业员工
        userLable: ''
      },
      // rules: {
      //   nickName: [{ required: true, message: '请输入用户姓名', trigger: 'change' },
      //     { validator: validateOrgName, trigger: 'blur' }],
      //   username: [{ required: true, message: '请输入用户账号', trigger: 'change' },
      //     { validator: this.$validateUsername, trigger: 'blur' }],
      //   pass: [{ validator: this.$validatePwd, trigger: 'blur' }],
      //   enabled: [{ required: true, message: '请输入授权状态', trigger: 'change' }]
      // },
    }
  },
  computed: {
    rules() {
      return {
        nickName: [{ required: true, message: '请输入用户姓名', trigger: 'blur' },
          { validator: validateOrgName, trigger: 'blur' }],
        username: [{ required: true, message: '请输入用户账号', trigger: 'blur' },
          { validator: this.$validateUsername, trigger: 'blur' }],
        pass: [{ required: !this.isEdit, message: '请输入密码', trigger: 'blur' },
          { validator: this.$validatePwd, trigger: 'blur' }],
        enabled: [{ required: true, message: '请输入授权状态', trigger: 'change' }],
        userLable: [{ validator: validateUseLabel, trigger: 'blur' }],
      }
    },
    isEdit() {
      return this.$route.name === 'users.staff.edit'
    }
  },
  mixins: [
    checkForm
  ],
  methods: {
    $validatePwd(rule, value, callback) {
      if (this.isEdit && !value) {
        return callback();
      }
      return validatePwd(rule, value, callback)
    },
    async $validateUsername(rule, value, callback) {
      if (/^[A-Za-z|\d]{6,20}$/.test(value)) {
        try {
          const params = {
            username: value,
            id: 0
          }
          if (this.isEdit) {
            params.id = this.$route.params.id
          }
          const { data } = await checkUsername(params)
          if (data !== true) {
            return callback(new Error('该账号已存在！'))
          }
          // if (!this.isEdit) {
          //   const { data } = await checkUsername({ username: value })
          //   if (data !== true) {
          //     return callback(new Error('该账号已存在！'))
          //   }
          // }
          return callback();
        } catch (err) {
          return callback(new Error('该账号已存在！'))
        }
      }
      return callback(new Error('请输入6-20位英文或数字！'))
    },
    async $saveForm() {
      const isValid = await this.$asyncCheckForm('basicForm')
      if (!isValid) {
        return
      }

      this.isPageLoading = true
      let func = addEmployee
      const params = {
        ...this.basicForm
      }
      if (this.$route.params.id) {
        func = updateEmployee
        params.id = this.$route.params.id
        if (!params.pass) {
          params.pass = this.OriginalData.originalPwd
        }
      }
      try {
        await func(params)
        this.isPageLoading = false
        this.$message.success('人员信息保存成功!')
        this.$router.go(-1)
      } catch (err) {
        this.isPageLoading = false
        this.$message.error(err.message || err.message || '保存员工信息出错')
      }
    },
    async getById() {
      const params = {
        id: this.$route.params.id
      }
      this.isPageLoading = true
      try {
        // const { data } = await getOrgByIdApi(params)
        const res = await getEmployeeInfo(params)
        const data = res.data || {}
        this.OriginalData = { ...data }
        this.basicForm = {
          ...this.basicForm,
          nickName: data.nickName,
          username: data.username,
          pass: data.pass,
          enabled: data.enabled,
          userLable: data.userLable
        }
        this.isPageLoading = false
      } catch (err) {
        console.log(err)
        this.isPageLoading = false
        this.$message.error(err.message || err.message || '拉取员工详细异常')
      }

    },
  },
  mounted() {
    if (this.$route.params.id) {
      this.id = this.$route.params.id
      this.getById()
    }
  },
};
</script>
